﻿using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Pay.Token.Controllers {
    /// <summary>
    ///广告
    /// </summary>
    [RoutePrefix("ad")]
    public class AdvertisementController : BaseController {
        private IAdvertisementService _iAdvertisementService;

        public AdvertisementController(IAdvertisementService iAdvertisementService) {
            _iAdvertisementService = iAdvertisementService;
        }

        /// <summary>
        /// 根据位置类型获得广告列表
        /// </summary>
        /// <returns></returns>
        [Route("get_ads"), HttpGet, AllowAnonymous]
        public ResultMessage<List<ad_dto>> GetAdvertisementList([FromUri]merch_advertisement_query_type query) {
            var result = _iAdvertisementService.AppGetAdvertisementList(query);
            return ResultToJson.Json(result);
        }

        /// <summary>
        /// 根据（多个）位置类型获得广告列表
        /// </summary>
        /// <returns></returns>
        [Route("get_ads_by_types"), HttpGet, AllowAnonymous]
        public ResultMessage<List<ad_type_dto>> GetAdListByTypes([FromUri]merch_advertisement_query_types query) {
            List<int> types = new List<int>();
            if (!string.IsNullOrEmpty(query.position_types))
                types = query.position_types.Split(',').Select(o => int.Parse(o)).ToList();
            var result = _iAdvertisementService.AppGetAdListByTypes(types);
            return ResultToJson.Json(result);
        }

        /// <summary>
        /// 获得广告详情（记录广告点击数）
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [Route("ad_click"), HttpPost, AllowAnonymous]
        public ResultMessage<advertisement_dto> GetAdvertisement([FromBody]merch_advertisement_query query) {
            var result = _iAdvertisementService.AppGetAdvertisement(query.id);
            return ResultToJson.Json(result);
        }
    }
}

